import MyModel
from MyModel import ManageRegions
from MyModel import ManageNation
import TwitterTool
import GoogleMapsTool
import time
import logging
from graphy.backends import google_chart_api
from graphy import common
from graphy import line_chart
import os
import sys
import json
from xml.dom.minidom import parseString
import unicodedata
os.path.join(os.path.split(__file__)[0], 'borders.rml')
class GatherController():
  
  
  lstStrSearchTerms = ["Fuck", "Shit", "Cunt", "Bastard"]
  #Terms are cached to prevent loads of lookups in the next step.
  term1 = lstStrSearchTerms[0]
  term2 = lstStrSearchTerms[1]
  term3 = lstStrSearchTerms[2]
  term4 = lstStrSearchTerms[3]
#Dictionary, keys are county names. lstValues are a list of tuples.
#The tuples contain one of the search terms, and a counter for it.
  countiesSwearingDict = {
  "Carlow": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Cavan": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Clare": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Cork": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Donegal": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Dublin": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Galway": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Kerry": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Kildare": [(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Kilkenny":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Laois":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Leitrim":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Limerick":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Longford":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Louth":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Mayo":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Meath":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Monaghan":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Offaly":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Roscommon":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Sligo":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Tipperary":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Waterford":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Westmeath":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Wexford":[(term1, 0), (term2, 0), (term3, 0), (term4, 0)],
  "Wicklow" :[(term1, 0), (term2, 0), (term3, 0), (term4, 0)]
  }
  dbRegionManager = ManageRegions()
  dbNationManager = ManageNation()
  lstFailingLocations = ["Liverpool", "Wigan", "Cymru", "Wales", "Milford Haven", "Fishguard", "Pembroke", "UK", "United Kingdom", "Britain", "England", "Isle Of Man"]
  strGeocode = '%s,%s,%s' % (53.000, -8, '270km')
  intItemsToCollect = 200
  def Run(self):
    self.InitRegions()
    
    self.CheckAndRecordResults(
    GoogleMapsTool.GetAdminArea(TwitterTool.CollectTweets
    ( " OR ".join(self.lstStrSearchTerms),self.strGeocode,self.intItemsToCollect),self.lstFailingLocations)
    )
  
    self.WriteToDB()
  
  def InitRegions(self):
    strWeeklyTupleLst = []
    if self.dbRegionManager.CheckHasEntities() == False:
      lstInitValues = []
      lstBorders =  self.ReadBordersFromXML()  
      
     
      lstBorderVals = []
      
      strTupleLst= []
      strOfTuples = ""
      for i in self.lstStrSearchTerms:
        strTupleLst.append(str((i,0)))
        
      strOfTuples = "_".join(strTupleLst)
     
      for i in range(0,7):
        strWeeklyTupleLst.append(strOfTuples)

     
     
      for border in lstBorders:

         
        lats = []
        longs = []
        for item in border[1]:
          lats.append(item[1])
          longs.append(item[0])
          
        lat = float( sum(lats)/len(lats))
        long = float( sum(longs)/len(longs))
        lstInitValues.append( [("regionName",border[0] ), ("gpLocation", str(lat) + "," + str(long) ), ("tupleValues",strWeeklyTupleLst),("borders",MyModel.ReturnDBTextOfStr(GoogleMapsTool.EncodePolyLine(border[1])))] )
      for lstItem in lstInitValues:
        logging.info(lstItem[0])
   
      self.dbRegionManager.InitRegions(lstInitValues)
      self.dbNationManager.InitNation( [("tupleValues",strWeeklyTupleLst)])
    
    
  
    
    
    
  
  def ReadBordersFromXML(self): 
    file = open('borders.rml','r')
    #convert to string:
    data = file.read()
    #close file because we dont need it anymore:
    file.close()
    #parse the xml you got from the file
    dom = parseString(data)
    #dom.getElementsByTagName('Region')
    logging.warning("In RBX")
    lstTupleNmeBorders = []
    for node in  dom.getElementsByTagName('Region'):
     
      lstTupleBorders = []
      strCrdsLst = list(eval(node.getAttribute("border").strip()))#.split(",")

      for lineItem in  strCrdsLst:
        lats = []
        longs = []
        tempLst = lineItem.split(",")
        if tempLst[0] != u'':
          #logging.info(tempLst)
          x = float (str(tempLst[0]) )
          y = float(str(tempLst[1]))
          #lats.append
          lstTupleBorders.append((x,y))
      lstTupleNmeBorders.append((node.getAttribute("name").strip(),lstTupleBorders))
      
      #lstTupleNmeBorders.sort(key=lambda tup: tup[0])

    return lstTupleNmeBorders
      
  def CheckAndRecordResults(self,lstOfTextAndLocations):
    #logging.info("In CARR, received" + str (lstOfTextAndLocations))
    
    if lstOfTextAndLocations: 
      for lstItemOfLOLTAL in lstOfTextAndLocations:
        for key in self.countiesSwearingDict.iterkeys():
#          logging.info("Key was: " + key)
#          logging.info("Reported Location Was: " + lstItemOfLOLTAL[1] )
          if lstItemOfLOLTAL[1].find(key) != -1 or lstItemOfLOLTAL[1].find(key.lower()) != -1 :
            
            #numberValidCounties += 1
            for i, (swear) in enumerate(self.lstStrSearchTerms):
    
              for item in range(0, self.FindAllInString(lstItemOfLOLTAL[0], swear)):
#                logging.info(swear)
#                logging.info(lstItemOfLOLTAL[0])
#                logging.info("Tuple To Update: %d" % (i))
                newValue = self.countiesSwearingDict[key][i][1]
                newValue += 1
                self.countiesSwearingDict[key][i] = (self.countiesSwearingDict[key][i][0], newValue)
                newValue = 0
                #numberAccepted += 1
#                logging.info("Location was: " + lstItemOfLOLTAL[1])
#                logging.info(key + str(self.countiesSwearingDict[key]))
    
              #acceptedList.append(resultId)
            break
          
            

  def FindAllInString(self, strTBSrch, subStr):
    start = 0
    matches = 0
    while start != -1:
      start = strTBSrch.find(subStr, start)
      if start != -1:
        start += len(subStr)
        matches += 1

    start = 0
    while start != -1:

      start = strTBSrch.find(subStr.lower(), start)
      if start != -1:
        start += len(subStr)
        matches += 1
    #logging.info(matches)
    return matches
                
  def WriteToDB(self):
    intDay = time.localtime()[6]
    #["Wexford",("tuplelstValues",lstInnerAROL[1][intDay]),("chartNow",chart.display.Url(310, 150))
    lstUpdateValues = []
    lstTotalValues = []
    lstToHoldTotalAsStr = []
    for count in range(0,len(self.lstStrSearchTerms)):
      lstTotalValues.append((self.lstStrSearchTerms[count],0))
    strChartImg = ""
    for lstItemOfTV in  self.dbRegionManager.ReturnPropertyOfRegions("tupleValues"):
       #logging.info(lstItemOfTV[0])
      
       lstOldRegTuples = []
       
       if lstItemOfTV [1]:
         lstValues = []
         bottomLabels = []
         for strItem in lstItemOfTV[1][intDay].rsplit("_"):
           tupleResult = tuple(eval(strItem))
           lstOldRegTuples.append((tupleResult[0], int(tupleResult[1])))
    
         
         lstNewRegTuples = self.countiesSwearingDict[lstItemOfTV[0]]  
         lstToHoldTupleAsStr = []
    
         for i, (oldTuple, newTuple) in enumerate(zip(lstOldRegTuples, lstNewRegTuples)):
    
            lstOldRegTuples[i] = (oldTuple[0], oldTuple[1] + newTuple[1]) #update the tuples with vals from dictionary
            lstValues.append(lstOldRegTuples[i][1])#collect these for graphing
            bottomLabels.append(lstOldRegTuples[i][0])#collect the search terms for graphing
            lstToHoldTupleAsStr.append(str(lstOldRegTuples[i]))#build a list of the new tuples as strings.
         
         for i,(itemV,itemTV) in enumerate(zip(lstValues,lstTotalValues)):# update national totals
           
           lstTotalValues[i] = (lstTotalValues[i][0], itemTV[1] +itemV)
           
         lstItemOfTV[1][intDay] = "_".join(lstToHoldTupleAsStr)# turn list into acceptable string for db
         
       chart = None  
       if sum(lstValues) != 0:
         chart = google_chart_api.BarChart (list( float ((i) /float(sum(lstValues))) * 100/1  for i in lstValues ))
         chart.bottom.labels = bottomLabels
         chart.left.min = 0
         chart.left.max = 100
         chart.left.labels = [0, 20, 40, 60,80,100]
         chart.left.label_positions = [0, 20, 40, 60,80,100]
         totalAxis =  chart.AddAxis(common.AxisPosition.BOTTOM, common.Axis())
         totalAxis.min = 1
         totalAxis.max = 5
         totalAxis.labels = list("Total: %d"%(item) for item in lstValues)
         
         strChartImg = chart.display.Img(310, 150)
         
      
       else:
         strChartImg = '<img src = "/static/ErrorNoData.jpg">'
         
         
       
       lstUpdateValues.append([lstItemOfTV[0],("tupleValues", lstItemOfTV [1]),("nowChart",strChartImg)])
    self.dbRegionManager.UpdateRegions(lstUpdateValues)
    
    #testList = "_".join(str(item) for item in lstTotalValues) 
   
    lstOfJstVals = []
    for itemLTV in lstTotalValues:
      lstOfJstVals.append(itemLTV[1])
    
    logging.info("Ltv: " + str(lstTotalValues))
    
    
    if sum(lstOfJstVals) != 0: 
      chart = google_chart_api.BarChart (list( float ((itemOJV) /float(sum(lstOfJstVals))) * 100/1  for itemOJV in lstOfJstVals ))
  
      chart.bottom.labels = bottomLabels
      chart.left.min = 0
      chart.left.max = 100
      chart.left.labels = [0, 20, 40, 60,80,100]
      chart.left.label_positions = [0, 20, 40, 60,80,100]
      totalAxis =  chart.AddAxis(common.AxisPosition.BOTTOM, common.Axis())
      totalAxis.min = 1
      totalAxis.max = 5
      totalAxis.labels = list("Total: %d"%(item) for item in lstOfJstVals)
      
      strChartImg = chart.display.Img(310, 150)
      logging.info(strChartImg)
      logging.info(lstOfJstVals)
  
    for item in lstTotalValues:
      lstToHoldTotalAsStr.append(str(item))
    
    nationTupleList = self.dbNationManager.ReturnPropertyOfNation("tupleValues")
    nationTupleList[intDay] = "_".join(lstToHoldTotalAsStr)
    logging.info(lstToHoldTotalAsStr)
    self.dbNationManager.UpdateNation([("tupleValues",nationTupleList),("nowChart",strChartImg)])
    
    
    
        
        